package com.zkca.xf.presenter;

import android.support.annotation.NonNull;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.zkca.xf.base.BasePresenter;
import com.zkca.xf.manager.DataManager;
import com.zkca.xf.model.javabean.UserId;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.view.LoginView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by li on 2018/7/31.
 */

public class LoginPresenter extends BasePresenter {


    private DataManager dataManager;
    private LoginView loginView;

    public LoginPresenter(LifecycleProvider<ActivityEvent> provider, LoginView loginView) {
        super(provider);
        this.loginView = loginView;
        dataManager = DataManager.getInstance();
    }

    //获取人员信息（userid,orgid）
    public void Login(Map<String, String> map) {
        dataManager.login(map)
                .subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<UserId>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<UserId>() {// 订阅
                    @Override
                    public void onNext(UserId userId) {
                        loginView.getUserId(userId);
                    }

                    @Override
                    public void onError(Throwable e) {
                        loginView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        loginView.hideProgressDialog();
                    }
                });
//                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
//                .compose(getProvider().<ExpressInfo>bindUntilEvent(ActivityEvent.DESTROY)) // onDestroy取消订阅
//                .subscribe(new DefaultObserver<UserId>() {  // 订阅
//                    @Override
//                    public void onNext(@NonNull UserId userId) {
//                        loginView.getUserId(userId);
//                    }
//
//                    @Override
//                    public void onError(@NonNull Throwable e) {
//                        expressView.showError(e.getMessage());
//                        expressView.hideProgressDialog();
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        expressView.hideProgressDialog();
//                    }
//                });
    }
}
